﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SinBiota.Model.Entities.Project>" %>

<%@ Import Namespace="Localization" %>
<%@ Import Namespace="SinBiota.Model.Entities" %>
<%@ Import Namespace="Sinbiota.Web.Extensions" %>
<%@ Import Namespace="Sinbiota.Web.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <%: Strings.Manage %>
    <%:Strings.Project %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        <%: Strings.Manage %>
        <%:Strings.Project %></h2>
    <% Html.EnableClientValidation(); %>
    <h3><%: Strings.Project %></h3>
    <div class="display-field">
        <span class="project_name"><%: Model.Name%></span>
    </div>
    <fieldset>
    <div class="row">
        <div class="four columns">
        <span class="display-label"><%: Messages.AddProjectMember %>:</span>
        </div>
        <div class="eight columns left">
        <a class="<%: ViewData["actionButtonClass"] %>" href="javascript:addExistingUser('<%: ProjectRole.Member %>')"><%: Strings.ExistingUser %></a> 
        <a class="<%: ViewData["actionButtonClass"] %>" href="javascript:addNewUser('<%: ProjectRole.Member %>')"><%: Strings.NewUser%></a>
        </div>
    </div>
    <div class="row" style="margin-top:1em;">
        <div class="four columns">
        <span class="display-label">
            <%: Messages.AddProjectCoordinator %>:</span> 
        </div>
        <div class="eight columns left">
            <a class="<%: ViewData["actionButtonClass"] %>" href="javascript:addExistingUser('<%: ProjectRole.Coordinator %>')"><%: Strings.ExistingUser %></a>
            <a class="<%: ViewData["actionButtonClass"] %>" href="javascript:addNewUser('<%: ProjectRole.Coordinator %>')"><%: Strings.NewUser%></a>
        </div>
    </div>
    </fieldset>
    
    <div id="ExistingUser">
        <fieldset>
            <legend><span id='existing_user_legend'><%: Messages.AddProjectMember %></span></legend>
            <% using (Ajax.BeginForm("AddUserToProject", null, new AjaxOptions { OnComplete = "AddProjectMemberCompleted", LoadingElementId = "AddMemberWaiting" }, new { Id = "AddProjectMemberForm" }))
               { %>
            <%: Html.HiddenFor(x => x.Id) %>
            <%: Html.Hidden("ProjectRole",ProjectRole.Member) %>
            <div class="display-field">
                <%: Messages.SelectUserToAdd %>:
                <select id="UserId" name="UserId">
                </select><span id="UserSelectWaiting"><img src='<%: Url.WaitingImage() %>' alt="Busy" /></span>
            </div>
            <p>
                <input type="submit" value="<%: Strings.Save %>" />
                <a class="<%: ViewData["actionButtonClass"] %>" onclick="javascript:cancelAddUser();"><%:Strings.Cancel %></a>
            </p>
            <% } %>
        </fieldset>
    </div>
    <div id="UserForm">
        <fieldset>
            <legend><span id='new_user_legend'><%: Strings.NewUser%></span></legend>
            <%  using (Ajax.BeginForm("Create", "User", new AjaxOptions { OnComplete = "OnCompleteCreateUser", LoadingElementId = "UserFormWaiting", OnFailure = "OnAjaxFailure" }))
                {%>
            
            <% Html.RenderPartial("EditorTemplates/CreateUserModel", new CreateUserModel()); %>
            <p>
                <input type="submit" value="<%: Strings.Save %>" />
                <a class="<%: ViewData["actionButtonClass"] %>" onclick="javascript:cancelAddUser();"><%:Strings.Cancel %></a>
                <span id="UserFormWaiting" style="visibility: hidden;">
                    <img src='<%: Url.WaitingImage() %>' alt="Busy" /></span>
            </p>
        </fieldset>
        <% }  %>
    </div>

    <h3><%: Strings.Coordinators %></h3>
    <div id="project_coordinators">
    </div>
    <h3><%: Strings.Members %></h3>
    <div id="project_members">
    </div>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" src="<%: Url.Content("~/Scripts/Custom/BiotaAjaxFunctions.js") %>"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#ExistingUser").hide();
            $("#UserForm").hide();
            populateProjectUsersTables();
        });
        function populateProjectUsersTables() {
            $('#project_members').activity();
            $('#project_coordinators').activity();
            $.post(approot + '/Project/GetMembers/', {id:<%: Model.Id %>},
                function (data) {
                    $('#project_members').html(data);
                }
            );            
            $.post(approot + '/Project/GetCoordinators/', {id:<%: Model.Id %>},
                function (data) {
                    $('#project_coordinators').html(data);
                }
            );            
        }
        function addExistingUser(projectrole) {
            setProjetRole(projectrole);
            if (projectrole == '<%: ProjectRole.Member %>') {
                $("#existing_user_legend").html('<%: Messages.AddProjectMember %>');
            }
            else {
                $("#existing_user_legend").html('<%: Messages.AddProjectCoordinator %>');
            }
            $("#ExistingUser").show('slow');
            $("#UserForm").hide('slow');
            populateSelect("User");
        }
        function AddProjectMemberCompleted(content) {
            var result = eval('(' + content.get_data() + ')');
            if (result.Success) {
                InformationMessage('<b>'+result.Header + "</b><br>" + result.Message, result.Seconds);
                $("#ExistingUser").hide('slow');
                populateProjectUsersTables();
            }
            else {
                ErrorMessage('<b>'+result.Header + "</b><br>" + result.Message, result.Seconds);
            }
        }

        function cancelAddUser() {
            $("#ExistingUser").hide('slow');
            $("#UserForm").hide('slow');
        }

        function addNewUser(projectrole) {
            setProjetRole(projectrole);
            if (projectrole == '<%: ProjectRole.Member %>') {
                $("#new_user_legend").html('<%: Strings.NewUser %> - <%: Messages.AddProjectMember %>');
            }
            else {
                $("#new_user_legend").html('<%: Strings.NewUser %> - <%: Messages.AddProjectCoordinator %>');
            }
            $("#UserForm").show('slow');
            $("#ExistingUser").hide('slow');
        }

        function OnCompleteCreateUser(content) {
            var result = eval('(' + content.get_data() + ')');
            OnCompleteCreate(content, 'User', null, submitAddExistingUserForm);
        }
        function submitAddExistingUserForm() {
            $("#UserForm").hide('slow');
            $("#AddProjectMemberForm").submit();
        }
        function setProjetRole(projectrole) {
            $("#ProjectRole").val(projectrole);
        }
    </script>
</asp:Content>
